<?php
/**
 * (ajax) 品类状态预览
 */
namespace App\Admin\Controllers\Products;

use App\Admin\Ajax\Products\SkuReviewAjax;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Illuminate\Support\Facades\Request;

class SkuReviewController extends AdminController{

    protected $title = '产品预览';

    protected $filters = [
        'classify_id' => null,
        'sku' => null,
        'status' => null,
    ];

    protected function grid()
    {
        $filtersField = array_keys($this->filters);
        $filters = $this->_getFilters($filtersField);
        $this->_initFilters($filters);
        $query = $this->_initQuery($filters);
        $paginator = $this->_paginator($query);

        $action = getAction($filtersField);
        $filtersBlade = [];
        $filtersBlade['classify_id'] = getOptions(ProductClassify::options(), $this->filters['status']);
        $filtersBlade['status'] = getOptions(ProductClassify::$_status, $this->filters['status']);
        $filtersBlade['sku'] = $this->filters['sku'];

        return view('products.sku-review.index', compact('paginator','action','filtersBlade'));
    }

    private function _getFilters($filtersField){
        $filters = [];
        foreach ($filtersField as $field){
            $filters[$field] = request($field);
        }
        return $filters;
    }

    private function _initFilters($filters){
        foreach ($filters as $field => $filter){
            $this->filters[$field] = $filter;
        }
    }

    private function _initQuery($filters){

        $query = ProductClassify::query()
            ->orderBy('id', 'DESC');

        foreach ($filters as $field => $filter){

            if(!$filter)continue;

            if($field == 'status'){
                $query->where('status',$filter);
            }
            if($field == 'classify_id'){
                $query->where('id',$filter);
            }
            if($field == 'sku'){
                $query->whereHas('sku', function ($query) use($filter){
                    $query->where('psku_code', 'like', "%{$filter}%")
                        ->orWhere('psku_cn', 'like', "%{$filter}%");
                });
            }
            //if(in_array($field, ['classify_id'])){
            //    $query->where($field,$filter);
            //}
        }

        return $query;
    }

    private function _paginator($query){
        return $query->paginate((int) (request("per_page",3)))->appends(Request::input());
    }

    public function ajax(){
        $action = request('action');
        $param = request('param');
        $ajax = new SkuReviewAjax();
        return $param ? call_user_func([$ajax,$action],$param) : call_user_func([$ajax,$action]);
    }

    /*protected function grid(){

        Admin::style($this->_style());
        $grid = new Grid(new ProductClassify());
        $grid->model()->orderByDesc('id');

        $grid->column('title','品类')->expand(function ($model) use($grid){
            $skuList = ProductSku::query() ->where('classify_id', $model->id)->get();
            return view('products.sku-review.index', compact('skuList'));
        })->style('width: 300px; display: block; border-bottom: 0px solid #f4f4f4; overflow: hidden; text-overflow: ellipsis;')->totalRow('合计');

        $grid->column('season','季节性');

        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->expandFilter();
        $grid->paginate(10);
        //$grid->fixColumns(2, 0);
        //$grid->disableExport();
        //$grid->exporter(new StockupExporter());

        //$grid->filter(function($filter){
        //    $filter->column(1/2, function ($filter) {
        //        $filter->where(function ($query) {
        //            $query->whereHas('sku', function ($query) {
        //                $query->where('psku_code', 'like', "%{$this->input}%")
        //                    ->orWhere('psku_cn', 'like', "%{$this->input}%");
        //            });
        //        }, 'SKU','sku8')->placeholder('模糊查询SKU，名称');
        //        $filter->equal('psku_id', '产品')->select(ProductSku::optionsCn());
        //        $filter->equal('area_id','站点')->select(SysArea::options());
        //
        //    });
        //    $filter->column(1/2, function ($filter) {
        //        $filter->equal('classify.season', '季节性')->select(ProductClassify::$_season);
        //        $filter->equal('classify_id', '品类')->select(ProductClassify::options());
        //    });
        //});
        return $grid;
    }

    protected function _style(){
        return <<<STYLE

    .table > thead > tr > th, .table > tbody > tr > td {
        white-space: nowrap;
    }
    .table-responsive {
        max-height: calc(80vh);
    }

STYLE;
    }*/

}
